import { mapMutations, mapActions, mapState } from "vuex";
import { typeClassName } from "@/pages/config/classNameMap";
import _ from "lodash";
const requireComponent = require.context("./", true, /[A-Z]\w+\.(vue)$/);
const comp = {};
requireComponent.keys().forEach((fileName) => {
  if (fileName != "./index.vue") {
    const componentConfig = requireComponent(fileName);
    const name = componentConfig.default.name;
    const componentName = _.camelCase(
      name
        .split("/")
        .pop()
        .replace(/\.\w+$/, "")
    );
    comp[componentName] = componentConfig.default || componentConfig;
  }
});
Object.keys(comp).map((key) => {
  comp[key].components = comp;
});
export default {
  components: comp,
  data() {
    return {
      vms: {}, //页面组件vue实例
      completeLoad: false,
      me: this,
      watchs: [],
      //模型
      models: {
        //模型

        collectionPoint: {
          ...typeClassName["collectionPoint"],
        },
        product: {
          ...typeClassName["product"],
        },
        techOverLimit: {
          ...typeClassName["techOverLimit"],
        },
        labOverLimit: {
          ...typeClassName["labOverLimit"],
        },
      },
      productOptions: [],
      dataSet: {
        //数据集
        treeData: [],
        treeCurrentData: [],
        formData: {},
        tableData: [],
        tableData1: [],
        tableData2: [],
      },
    };
  },
  provide() {
    return {
      anc: this,
    };
  },
  computed: {},
  created() {
    Object.keys(this.models).map((k) => {
      this.models[k].clz = this.$modelObject.create(this.models[k].modelType);
      this.models[k].clzs = this.$modelObject.create(
        this.models[k].modelType + "Collection"
      );
    });
  },
  mounted() {
    this.watchs.map((item) => {
      this.$watch(item.target, item.fn, { deep: item.deep });
    });
    this.$nextTick(() => {
      console.log(comp, "comp");
      this.unwatch = this.$watch("vms", (newValue, oldValue) => {
        let flag = true;
        Object.keys(comp).map((k) => {
          if (!Object.keys(this.vms).includes(k)) {
            flag = false;
          }
        });
        this.completeLoad = flag;
      });
      this.init();
      if (process.env.NODE_ENV === "development") {
        console.log({
          me: this,
          vms: this.vms,
          completeLoad: this.completeLoad,
        });
      }
    });
  },
  destroyed() {
    this.unwatch();
  },
  methods: {
    init() {
      this.getProductOptions();
      this.getStatisticsByProduct();
    },
    loadPage(pageNavigation, filter) {
      Promise.all([
        this.loadPage1(),
        this.loadPage2(),
      ]).then((res) => {
        this.dataSet.tableData = res[0].concat(res[1]);
        // this.vms.mainTable.navigation.page.total =
        //   this.dataSet.tableData.length;
      });
    },
    getStatisticsByProduct() {
      let data = {
        startTime: this.startTime,
        endTime: this.endTime,
      }
      this.$api.inspection.statisticsByProduct(data).then((res) => {
       
        this.barData.categories = res.product;

        this.barData.values = res.values;
        this.lineData.categories = res.product;

        this.lineData.values = res.values;
      });
    },

    getProductOptions() {
      let data = {
        className: this.models.product.className,
        type: this.models.product.type,
      };
      this.productOptions = [];
      this._$query(this.models.product.clzs, data).then((res) => {
        if (res) {
          this.models.product.clzs.records.forEach((item) => {
            this.productOptions.push({
              label: item.values.sf_name,
              value: String(item.id),
            });
          });
        }
      });
    },

    loadPage1() {
      let newFilter = {
        className: this.models.techOverLimit.className,
        type: this.models.techOverLimit.type,
      };
      let filterString = this._$setFilteStr(newFilter, false);
      if (this.startTime != "") {
        filterString = filterString + " && " + this.dateOf;
      }
      return this._$query(
        this.models["techOverLimit"].clzs,

        filterString
      ).then((res) => {
        return (this.dataSet.tableData1 =
          this.models["techOverLimit"].clzs.valueRecords);
      });
    },
    loadPage2() {
      let newFilter = {
        className: this.models.labOverLimit.className,
        type: this.models.labOverLimit.type,
      };
      let filterString = this._$setFilteStr(newFilter, false);
      if (this.startTime != "") {
        filterString = filterString + " && " + this.dateOf;
      }
      return this._$query(
        this.models["labOverLimit"].clzs,

        filterString
      ).then((res) => {
        return (this.dataSet.tableData2 =
          this.models["labOverLimit"].clzs.valueRecords);
      });
    },
  },
};
